package com.ytjj.qmyx.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.ytjj.common.api.CommonPage;
import com.ytjj.qmyx.admin.dao.LabelDao;
import com.ytjj.qmyx.admin.eunm.LabelTypeEnums;
import com.ytjj.qmyx.admin.model.request.LabelRequest;
import com.ytjj.qmyx.admin.model.response.LabelResponse;
import com.ytjj.qmyx.admin.service.LabelService;
import com.ytjj.qmyx.users.model.Label;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class LabelServiceImpl implements LabelService {
    @Resource
    private LabelDao labelDao;

    @Override
    public CommonPage list(LabelRequest labelRequest) {
        PageHelper.startPage(labelRequest.getPageNum(), labelRequest.getPageSize());
        List<Label> list = labelDao.list();
        if (CollectionUtils.isEmpty(list)) {
            return CommonPage.restPage(new ArrayList<>());
        }
        List<LabelResponse> responses = new LinkedList<>();
        list.stream().forEach(item -> {
            LabelResponse response = new LabelResponse();
            BeanUtils.copyProperties(item, response);
            response.setTypeStr(LabelTypeEnums.getTypeByCode(item.getType()));
            responses.add(response);
        });
        CommonPage page = CommonPage.restPage(list);
        page.setList(responses);
        return page;
    }

    @Override
    public int insertLabel(LabelRequest labelRequest) {
        labelRequest.setCreateTime(new Date());
        int count = labelDao.insertLabel(labelRequest);
        return count;
    }

    @Override
    public int updateLabel(LabelRequest labelRequest) {
        labelRequest.setUpdateTime(new Date());
        int count = labelDao.updateLabel(labelRequest);
        return count;
    }

    @Override
    public int deleteLabel(Integer id) {
        return labelDao.deleteLabel(id);
    }
}
